﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FireControlSystem : MonoBehaviour
{
    public bool isRun;//是否运行
    public float fireRate = 1f;//射击频率
    public float firePower=1000f;//射击力度
    public Transform firePosObj;//发射点位置
    public GameObject fireFlash;//开炮特效
    private float timer;
    private int index_bullet;//炮弹序号
    void Awake()
    {
        if(firePosObj==null)
            firePosObj=transform.Find("TankFree_Tower/TankFree_Canon/FirePos");
    }

    // Update is called once per frame
    void Update()
    {
        if(!isRun)
            return;
        timer+=Time.deltaTime;
        if(timer>fireRate)
        {
            timer = 0;
            //实例化子弹及火焰
            ObjectPool.objArray[index_bullet].GetComponentInChildren<MeshRenderer>().enabled = true;
            //关闭忽略物体运算开关
            ObjectPool.objArray[index_bullet].GetComponent<Rigidbody>().isKinematic = false;
            ObjectPool.objArray[index_bullet].GetComponent<SphereCollider>().enabled = true;
            ObjectPool.objArray[index_bullet].GetComponent<Rigidbody>().AddForce(transform.forward * firePower * 1000f);//参照父物体坐标施加力
            if (++index_bullet > ObjectPool.objArray.Length -1)
                index_bullet = 0;
            Instantiate(fireFlash, firePosObj.position, firePosObj.rotation);
        }
    }
}
